Imports System.Configuration
Imports System.Xml
Public Class FrmConfiguration
    Public Dtable As DataTable
    Dim doCrypt As Encrypt
    Private Sub SetConnectionString()
        Try
            Dim cdata As New Data
            cdata.GetConfigData()
            If cdata.UseTrust = True Then
                My.MySettings.Default("db_VirtualDeskConnectionString") = cdata.TrustConnStr
            Else
                My.MySettings.Default("db_VirtualDeskConnectionString") = cdata.ConnStr
            End If

            My.MySettings.Default.Save()

        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Virtual Desk", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Err.Clear()

        End Try
    End Sub
    Public Function ReadAppSettings()
        Dtable = New DataTable
        Dim clm As DataColumn
        clm = New DataColumn

        clm.ColumnName = "Configuration Name"
        clm.Caption = "Configuration Name"
        Dtable.Columns.Add(clm)
        clm = New DataColumn
        clm.Caption = "Configuration Value"
        clm.ColumnName = "Configuration Value"
        Dtable.Columns.Add(clm)
        '  AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 
        ' This will get the app.config file path from Current application Domain
        Dim XmlDoc As New XmlDocument()
        ' Load XML Document
        XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
        ' Navigate Each xml Element of app.Config file
        Dim dr As DataRow
        For Each xElement As XmlElement In XmlDoc.DocumentElement
            If xElement.Name = "appSettings" Then

                ''Loop each node of appSettings Element 
                ' xNode.Attributes(0).Value , Mean First Attributes of Node , KeyName Portion
                ' xNode.Attributes(1).Value , Mean Second Attributes of Node,KeyValue Portion
                For Each xNode As XmlNode In xElement.ChildNodes
                    dr = Dtable.NewRow
                    If xNode.Attributes(0).Value = "DatabasePassword" Then
                        doCrypt = New Encrypt
                        dr(0) = xNode.Attributes(0).Value
                        dr(1) = doCrypt.decryption(xNode.Attributes(1).Value)
                    Else
                        dr(0) = xNode.Attributes(0).Value
                        dr(1) = xNode.Attributes(1).Value
                    End If
                    Dtable.Rows.Add(dr)
                    ''MsgBox(xNode.Attributes(0).Value & ": " & xNode.Attributes(1).Value)
                    'If xNode.Attributes(0).Value = KeyName Then
                    '    xNode.Attributes(1).Value = KeyValue
                    'End If
                Next
            End If
        Next
        Me.dgConfiguration.DataSource = Dtable
        'Dim i As Integer
        'Dim cellformatType As System.Type

        'For i = 0 To Me.dgConfiguration.RowCount - 1
        '    If Me.dgConfiguration.Rows(i).Cells(1).Value = "DatabasePassword" Then
        '        Me.dgConfiguration.Rows(i).Cells(1).FormattedValueType = ""



        '    End If
        'Next
        Return True
        ' Save app.config file
        ''XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
    End Function

    Public Function UpdateAppSettings(ByVal KeyName As String, ByVal KeyValue As String)

        '  AppDomain.CurrentDomain.SetupInformation.ConfigurationFile 
        ' This will get the app.config file path from Current application Domain
        Dim XmlDoc As New XmlDocument()
        ' Load XML Document
        XmlDoc.Load(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
        ' Navigate Each xml Element of app.Config file
        For Each xElement As XmlElement In XmlDoc.DocumentElement
            If xElement.Name = "appSettings" Then
                For Each xNode As XmlNode In xElement.ChildNodes
                    If xNode.Attributes(0).Value = KeyName Then
                        If KeyName = "DatabasePassword" Then
                            doCrypt = New Encrypt
                            xNode.Attributes(1).Value = doCrypt.encryption(KeyValue)
                        Else
                            xNode.Attributes(1).Value = KeyValue
                        End If
                    End If
                Next
            End If
        Next
        XmlDoc.Save(AppDomain.CurrentDomain.SetupInformation.ConfigurationFile)
        SetConnectionString()
        Return True
    End Function

    Private Sub FrmConfiguration_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        ''  MsgBox(My.MySettings.Default.db_VirtualDeskConnectionString)
    End Sub

    Private Sub FrmConfiguration_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ReadAppSettings()
        Me.dgConfiguration.Columns(0).ReadOnly = True
        Me.dgConfiguration.Columns(0).Width = 200
        Me.dgConfiguration.Columns(1).Width = 200

    End Sub

    Private Sub dgConfiguration_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles dgConfiguration.CellBeginEdit
        If Me.dgConfiguration.Rows(e.RowIndex).Cells(0).Value = "SQLServerBackupDirectory" Then
            Dim dgFolder As New FolderBrowserDialog
            dgFolder.RootFolder = Environment.SpecialFolder.ProgramFiles
            dgFolder.ShowDialog()
            If dgFolder.SelectedPath.Trim <> "" Then
                Me.dgConfiguration.Rows(e.RowIndex).Cells(1).Value = dgFolder.SelectedPath
            End If
        End If
        If Me.dgConfiguration.Rows(e.RowIndex).Cells(0).Value = "SQLServerDataDirectory" Then
            Dim dgFolder As New FolderBrowserDialog
            dgFolder.RootFolder = Environment.SpecialFolder.ProgramFiles
            dgFolder.ShowDialog()
            If dgFolder.SelectedPath.Trim <> "" Then
                Me.dgConfiguration.Rows(e.RowIndex).Cells(1).Value = dgFolder.SelectedPath
            End If
        End If


        ''DatabaseServer
        If Me.dgConfiguration.Rows(e.RowIndex).Cells(0).Value = "DatabaseServer" Then
            pnlloading.Visible = True
            pnlloading.Update()
            Me.Update()
            Dim frmsql As New gFrmSQLServer
            Application.DoEvents()
            If frmsql.getNames() = True Then
                frmsql.ShowDialog()
            End If
            ''
            '' Application.DoEvents()

            If frmsql.sqlselected <> "" And frmsql.DialogResult = Windows.Forms.DialogResult.OK Then
                Me.dgConfiguration.Rows(e.RowIndex).Cells(1).Value = frmsql.sqlselected
            End If
            Me.pnlloading.Visible = False
        End If
        If Me.dgConfiguration.Rows(e.RowIndex).Cells(0).Value = "DatabasePassword" Then
            doCrypt = New Encrypt
            Dim passval As String = Me.dgConfiguration.Rows(e.RowIndex).Cells(1).Value
            If passval.Split("-").Length > 3 Then
                Me.dgConfiguration.Rows(e.RowIndex).Cells(1).Value = doCrypt.decryption(passval)

            End If
            ''  MsgBox(doCrypt.encryption(passval))

        End If
    End Sub

    Private Sub dgConfiguration_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgConfiguration.CellContentClick

    End Sub

    Private Sub dgConfiguration_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgConfiguration.CellEndEdit
        If Me.dgConfiguration.Rows(e.RowIndex).Cells(0).Value = "DatabasePassword" Then
            doCrypt = New Encrypt
            Dim passval As String = Me.dgConfiguration.Rows(e.RowIndex).Cells(1).Value
            ''  MsgBox(doCrypt.encryption(passval))
            Me.dgConfiguration.Rows(e.RowIndex).Cells(1).Value = doCrypt.encryption(passval)
            Me.UpdateAppSettings(Me.dgConfiguration.Rows(e.RowIndex).Cells(0).Value, Me.dgConfiguration.Rows(e.RowIndex).Cells(1).Value)
            Exit Sub
        Else
            Me.UpdateAppSettings(Me.dgConfiguration.Rows(e.RowIndex).Cells(0).Value, Me.dgConfiguration.Rows(e.RowIndex).Cells(1).Value)

        End If
    End Sub

    Private Sub dgConfiguration_CellLeave(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgConfiguration.CellLeave

    End Sub

    Private Sub dgConfiguration_CellValueChanged(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgConfiguration.CellValueChanged
        'If e.ColumnIndex = 1 Then
        '' MsgBox(Me.dgConfiguration.Rows(e.RowIndex).Cells(0).Value & ": " & Me.dgConfiguration.Rows(e.RowIndex).Cells(1).Value)
        'End If


    End Sub
End Class